namespace CSharpPro._1016;

public class Demo2
{
   public void SortThreeNumbers()
    {
        Console.WriteLine("===== 三个整数从小到大输出 =====");
        try
        {
            Console.Write("请输入第一个整数: ");
            int x = int.Parse(Console.ReadLine() ?? "");
            
            Console.Write("请输入第二个整数: ");
            int y = int.Parse(Console.ReadLine() ?? "");
            
            Console.Write("请输入第三个整数: ");
            int z = int.Parse(Console.ReadLine() ?? "");
            
            int[] numbers = { x, y, z };
            Array.Sort(numbers);
            
            Console.WriteLine($"排序结果: {numbers[0]} < {numbers[1]} < {numbers[2]}");
        }
        catch (FormatException)
        {
            Console.WriteLine("输入错误，请确保输入的是整数！");
        }
    }

    // 2. 打印1-100之间所有不是7的倍数和不包含7的数字，并求和
    public void PrintNumbersWithoutSeven()
    {
        Console.WriteLine("===== 1-100之间非7倍数且不含7的数字 =====");
        int sum = 0;
        int count = 0;
        
        for (int i = 1; i <= 100; i++)
        {
            if (i % 7 != 0 && !i.ToString().Contains("7"))
            {
                Console.Write($"{i}\t");
                sum += i;
                count++;
                
                if (count % 5 == 0)
                    Console.WriteLine();
            }
        }
        
        Console.WriteLine($"\n\n这些数字的总和是: {sum}");
    }

    // 3. 打印九九乘法表
    public  void PrintMultiplicationTable()
    {
        Console.WriteLine("===== 九九乘法表 =====");
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}×{i}={i * j}\t");
            }
            Console.WriteLine();
        }
    }

    // 4. 求1000以内的所有完数
    public void FindPerfectNumbers()
    {
        Console.WriteLine("===== 1000以内的完数 =====");
        List<int> perfectNumbers = new List<int>();
        
        for (int num = 2; num <= 1000; num++)
        {
            int sum = 1;
            
            for (int i = 2; i <= num / 2; i++)
            {
                if (num % i == 0)
                {
                    sum += i;
                }
            }
            
            if (sum == num)
            {
                perfectNumbers.Add(num);
            }
        }
        
        Console.Write("1000以内的完数有: ");
        Console.WriteLine(string.Join(", ", perfectNumbers));
        
        foreach (int p in perfectNumbers)
        {
            List<int> factors = new List<int>();
            for (int i = 1; i < p; i++)
            {
                if (p % i == 0)
                    factors.Add(i);
            }
            Console.WriteLine($"{p} = {string.Join(" + ", factors)}");
        }
    }

    // 5. 统计正整数位数并逆序打印
    public  void AnalyzeNumber()
    {
        Console.WriteLine("===== 数字分析 =====");
        Console.Write("请输入一个正整数: ");
        string input = Console.ReadLine()?.Trim() ?? "";
        
        if (string.IsNullOrWhiteSpace(input) || !IsAllDigits(input))
        {
            Console.WriteLine("输入无效，请输入一个正整数！");
            return;
        }
        
        int digitCount = input.Length;
        char[] chars = input.ToCharArray();
        Array.Reverse(chars);
        string reversed = new string(chars);
        
        Console.WriteLine($"该数字是 {digitCount} 位数");
        Console.WriteLine($"逆序打印结果: {reversed}");
    }

    // 辅助方法：判断字符串是否全为数字
    private bool IsAllDigits(string str)
    {
        foreach (char c in str)
        {
            if (!char.IsDigit(c))
                return false;
        }
        return true;
    }

    // 6. 根据输入的行数打印菱形
    public void PrintDiamond()
    {
        Console.WriteLine("===== 打印菱形 =====");
        try
        {
            Console.Write("请输入菱形的行数(必须是奇数): ");
            int rows = int.Parse(Console.ReadLine() ?? "");
            
            if (rows % 2 == 0)
            {
                Console.WriteLine("错误：行数必须是奇数！");
                return;
            }
            
            int mid = rows / 2;
            
            // 上半部分（包括中间行）
            for (int i = 0; i <= mid; i++)
            {
                Console.Write(new string(' ', mid - i));
                Console.WriteLine(new string('*', 2 * i + 1));
            }
            
            // 下半部分
            for (int i = mid - 1; i >= 0; i--)
            {
                Console.Write(new string(' ', mid - i));
                Console.WriteLine(new string('*', 2 * i + 1));
            }
        }
        catch (FormatException)
        {
            Console.WriteLine("输入错误，请输入一个整数！");
        }
    }
}